/*
Copyright 2010  Murali Devi (profdevi@gmail.com)

This file is part of ComineGL Project.

ComineGL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ComineGL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ComineGL.  If not, see <http://www.gnu.org/licenses/>.

This code uses the irrlicht and irrklang libraries.  Please read
the libraries copyrights at 

	irrlicht library:  http://irrlicht.sourceforge.net
	irrklang library:  http://www.ambiera.com/irrklang

*/



//v0.14 copyright Comine.com 20101212U01
#include <stdarg.h>
#include "MStdLib.h"
#include "Global.h"
#include "ComineGLWindow.h"


//***********************************************
//** Module Elements
//***********************************************
static const int GBufferSize=1000;


///////////////////////////////////////////////
static bool GFontWrite(int key,int left,int top,int size,bool colorkey,const char *str)
	{
	// FInd Font Object
	MCGLFontInfo *fontinfo=GlobalFontList.Find(key);
	if(fontinfo==NULL)
		{
		MStdPrintf("**Unable to find font id %d\n",key);
		return NULL;
		}

	//=We now have a string in the 
	int i;
	for(i=0;str[i]!=0;++i)
		{
		ITexture *texture=fontinfo->GetChar(str[i]);
		if(texture==NULL)
			{
			texture=fontinfo->GetChar(' ');
			if(texture==NULL)
				{  continue; }
			}

		int width=texture->getSize().Width;
		int height=texture->getSize().Height;

		GlobalRefVideoDriver->draw2DImage(texture,rect<s32>(left+i*size,top,left+(i+1)*size,top+size)
				,rect<s32>(0,0,width,height),0,0,colorkey);
		}
	
	return true;
	}


//***********************************************
//** Exported Functions
//***********************************************
MDLLFUN int CGLWindowFPS(void)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return 0;
		}
	
	return (int)GlobalRefVideoDriver->getFPS();
	}


//////////////////////////////////////////////////////
MDLLFUN bool CGLWindowSetCaption(const char *fmt,...)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	char buf[GBufferSize];
	va_list args;
	va_start(args,fmt);

	#if _MSC_VER>=1300
	int length=vsnprintf_s(buf,GBufferSize-1,_TRUNCATE,fmt,args);
	#else
	int length=_vsnprintf(buf,GBufferSize-1,fmt,args);
	#endif // _MSC_VER

	va_end(args);

	wchar_t widebuf[GBufferSize];
	MStdSPrintf(widebuf,GBufferSize-1,L"%S",buf);
	GlobalRefDevice->setWindowCaption(widebuf);
	
	return true;
	}


////////////////////////////////////////////////////////////
MDLLFUN bool CGLWindowGetSize(int &width,int &height)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	rect<s32> rect;
	rect=GlobalRefVideoDriver->getViewPort();
	
	width=rect.getWidth();
	height=rect.getHeight();

	return true;
	}


////////////////////////////////////////////////////////////
MDLLFUN bool CGLWindowWrite(int fontid,int left,int top,int size,bool colorkey,const char *fmt,...)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	// Build up the string
	char buf[GBufferSize];
	va_list args;
	va_start(args,fmt);

	#if _MSC_VER>=1300
	int length=vsnprintf_s(buf,sizeof(buf)-1,_TRUNCATE,fmt,args);
	#else
	int length=_vsnprintf(buf,sizeof(buf)-1,fmt,args);
	#endif // _MSC_VER

	va_end(args);

	if(GFontWrite(fontid,left,top,size,colorkey,buf)==false)
		{
		MStdPrintf("**Unable to write string to window",__LINE__);
		return false;
		}

	return true;
	}


///////////////////////////////////////////////////
MDLLFUN bool CGLWindowBackRenderer(bool action(void) )
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	GlobalWindowBackRenderer=action;
	
	return true;
	}


///////////////////////////////////////////////////
MDLLFUN bool CGLWindowFrontRenderer(bool action(void) )
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	GlobalWindowFrontRenderer=action;

	return true;	
	}


////////////////////////////////////////////////////////////
MDLLFUN bool CGLWindowSetBackColor(unsigned char red,unsigned char green,unsigned char blue)
	{
	if(GlobalInitialized==false)
		{
		return false;
		}

	GlobalBackColor=SColor(255,red,green,blue);

	return true;
	}


///////////////////////////////////////////////////////////////
MDLLFUN bool CGLWindowCapture(const char *filename)
	{
	if(GlobalInitialized==false)
		{
		return false;
		}

	if(filename==NULL)
		{
		MStdPrintf("**NULL filename passed for file save\n");
		return false;
		}
	
	IImage *capimage=GlobalRefVideoDriver->createScreenShot();
	if(capimage==NULL)
		{
		MStdPrintf("**Unable to capture window\n");
		return false;
		}

	if(GlobalRefVideoDriver->writeImageToFile(capimage,filename)==false)
		{
		MStdPrintf("**Unable to write image to file %s\n",filename);
		capimage->drop();
		return false;
		}

	capimage->drop();
	return true;
	}


///////////////////////////////////////////////////
MDLLFUN bool CGLWindowDrawTexture(int texid,int x,int y,int width,int height,bool colorkey)
	{
	if(GlobalInitialized==false)
		{
		return false;
		}

	MCGLTextureInfo *texture=GlobalTextureList.Find(texid);
	if(texture==NULL)
		{
		MStdPrintf("**Unable to find texture %d\n",texid);
		return false;
		}

	ITexture *itexture=texture->GetTexture();

	int texwidth=itexture->getSize().Width;
	int texheight=itexture->getSize().Height;

	GlobalRefVideoDriver->draw2DImage(itexture,rect<s32>(x,y,x+width,y+height)
			,rect<s32>(0,0,texwidth,texheight),0,0,colorkey);

	return true;	
	}


